package com.my.wechat.service.impl;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.my.wechat.domain.entity.WeChatEntity;
import com.my.wechat.repository.WeChatRepository;
import com.sskj.core.annotation.ControllerLogExeTime;
import com.sskj.core.util.JsonUtil;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/wechat/", method = { RequestMethod.POST, RequestMethod.GET })
@Transactional
public class WeChatServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	WeChatRepository weChatRepository;

	@ControllerLogExeTime(description = "获取微信配置信息", log = false)
	@RequestMapping(value = "/config/get", method = { RequestMethod.GET })
	public WeChatEntity get() {
		WeChatEntity result = weChatRepository.findOne();

		if (null == result) {
			result = new WeChatEntity();
			weChatRepository.save(result);
		}
		logger.info(JsonUtil.json(result));
		return result;
	}

	@ControllerLogExeTime(description = "修改微信配置")
	@RequestMapping(value = "/config/update", method = { RequestMethod.POST, RequestMethod.PUT })
	@ApiOperation(value = "修改微信配置", httpMethod = "PUT")
	public void update(WeChatEntity weChatEntity) {

		String id = weChatEntity.getId();
		WeChatEntity entity = weChatRepository.get(id);
		entity.setAesKey(weChatEntity.getAesKey());
		entity.setAppId(weChatEntity.getAppId());
		entity.setKeyPath(weChatEntity.getKeyPath());
		entity.setMchId(weChatEntity.getMchId());
		entity.setMchKey(weChatEntity.getMchKey());
		entity.setSecret(weChatEntity.getSecret());
		entity.setSubAppId(weChatEntity.getSubAppId());
		entity.setSubMchId(weChatEntity.getSubMchId());
		entity.setToken(weChatEntity.getToken());
		weChatRepository.save(entity);

		logger.info(JsonUtil.json(entity));

	}

}
